package jlxy.ise.eroad;


import android.app.Application;
import android.content.Context;

import com.baidu.mapapi.SDKInitializer;
import com.google.gson.Gson;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import cz.msebera.android.httpclient.Header;
import jlxy.ise.eroad.bean.DriverInfo;
import jlxy.ise.eroad.bean.PackageInfo;
import jlxy.ise.eroad.bean.ServerRpsInfo;
import jlxy.ise.eroad.util.SingleToast;
import jlxy.ise.eroad.util.WebUtil;


/**
 * 应用的入口
 */
public class GdApp extends Application {

	private static final String TAG = GdApp.class.getSimpleName();



	public static Context ctx;


	List<PackageInfo.DataBean> myCompletedPkgList = new ArrayList<>();//我完成的订单
	public List<PackageInfo.DataBean> getMyCompletedPkgList() {
		return myCompletedPkgList;
	}

	List<PackageInfo.DataBean> waitPkgList = new ArrayList<>();//等待接单的订单
	public List<PackageInfo.DataBean> getWaitPkgList() {
		return waitPkgList;
	}




	@Override
	public void onCreate() {
		super.onCreate();
		ctx = GdApp.this;
		// 在使用百度地图SDK 各组间之前初始化 context 信息，传入 ApplicationContext
		SDKInitializer.initialize(this);
		initImageLoader(GdApp.this);
	}


	/**
	 * 初始化ImageLoader
	 */
	public static void initImageLoader(Context context) {
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context)
				.threadPoolSize(3).threadPriority(Thread.NORM_PRIORITY - 2)
						//.memoryCache(new WeakMemoryCache())
				.denyCacheImageMultipleSizesInMemory()
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.build();
		ImageLoader.getInstance().init(config);
	}


	/**
	 * 更新当前用户信息
	 */
	public void refCurrUser(String telephone,String passwd) {
		cleanCache();
		WebUtil.postLoginVerify(WebUtil.getRpLoginVerify(telephone,passwd), new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
				super.onSuccess(statusCode, headers, response);

				Gson gson = new Gson();
				try {
					ServerRpsInfo serverRpsInfo = gson.fromJson(response.toString(), ServerRpsInfo.class);
					String userStr = gson.toJson(serverRpsInfo.getData());//返回的是DriverInfo
					DriverInfo driverInfo = gson.fromJson(userStr,DriverInfo.class);

				} catch (Exception e) {
					SingleToast.showToast(getApplicationContext(), "GdAPP:"+e.getLocalizedMessage(), 2000);
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable, errorResponse);
				SingleToast.showToast(getApplicationContext(), "登录失败", 2000);
			}
		});
	}




	private DriverInfo.DataBean mDriverInfo  = null;
	public  DriverInfo.DataBean getDriverInfo() {
		return mDriverInfo;
	}
	public void setDriverInfo( DriverInfo.DataBean info) {
		mDriverInfo = info;
	}


	@Override
	public void onTerminate() {
		super.onTerminate();


	}

	public void cleanCache(){
		waitPkgList.clear();
		myCompletedPkgList.clear();
	}

}
